/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AgentTypeEditor.java
 *
 * Created on 2011-11-07, 23:33:42
 */
package pobicos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author marcin
 */
public class AgentTypeEditor extends javax.swing.JFrame {

 //   JList list;
    private DefaultListModel model_;
    
    /** Creates new form AgentTypeEditor */
    public AgentTypeEditor(final AdaptSysView view) {
        initComponents();
        model_ = new DefaultListModel();
        goalsList_.setModel(model_);
        
        addNewGoal.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                model_.addElement(Integer.toString(model_.size()+1)+"."+newGoalField.getText());
                newGoalField.setText("");
            }
        });
        deleteGoalButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                model_.removeElement(goalsList_.getSelectedValue());
            }
        });
        addTypeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ArrayList<String> tempGoalsArray = new ArrayList<String>();
                for(int i=0;i<model_.getSize();++i) {
                    tempGoalsArray.add((String)model_.getElementAt(i));
                }
                view.addTypeToList(new AgentType(agentTypeNameFiled.getText(),tempGoalsArray));
                view.AddTypeComboBox.addItem(agentTypeNameFiled.getText());
                view.AddTypeComboBox.setEnabled(true); 
                //clean all the fields
                agentTypeNameFiled.setText("");
                newGoalField.setText("");
                model_.clear();
            }
        });
        saveToFileButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                File f1 = new File("agentsList.txt");
                if (!f1.exists()) {
                    try {
                        PrintWriter out = new PrintWriter(new FileWriter("agentsList.txt"));
                        out.print(agentTypeNameFiled.getText() + ";");
                        out.print(model_.getSize() + ";");
                        for (int i = 0; i < model_.getSize(); ++i) {
                            if (i == model_.getSize() - 1) {
                                //write last char and begin new line
                                 out.println((String) (model_.getElementAt(i)));
                            } else {
                                out.print((String) (model_.getElementAt(i)) + ";");
                            }
                        }
                        out.close();
                        JOptionPane.showMessageDialog(null, "Agent type succesfully saved to file!\n");
                    } catch (IOException exc) {
                        JOptionPane.showMessageDialog(null, "Error while saving to file!\n");
                    }
                } else {
                    try {
                        PrintWriter out = new PrintWriter(new FileWriter("agentsList.txt",true));
                        out.print(agentTypeNameFiled.getText() + ";");
                        out.print(model_.getSize() + ";");
                        for (int i = 0; i < model_.getSize(); ++i) {
                           if (i == model_.getSize() - 1) {
                               //write last char and begin new line
                                 out.println((String) (model_.getElementAt(i)));
                            } else {
                                out.print((String) (model_.getElementAt(i)) + ";");
                            }
                        }                       
                        out.close();
                        JOptionPane.showMessageDialog(null, "Agent type succesfully saved to file!\n");
                    } catch (IOException exc) {
                        JOptionPane.showMessageDialog(null, "Error while saving to file!\n");
                    }
                }
                
                
            }
        });
 
        this.setLocationRelativeTo(view.getFrame());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        leftPanel = new javax.swing.JPanel();
        addNewGoal = new javax.swing.JButton();
        AgentTypeName = new javax.swing.JLabel();
        deleteGoalButton = new javax.swing.JButton();
        agentTypeNameFiled = new javax.swing.JTextField();
        newGoalField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        goalsList_ = new javax.swing.JList();
        saveToFileButton = new javax.swing.JButton();
        addTypeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        close = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);
        setName("Form"); // NOI18N

        leftPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        leftPanel.setName("leftPanel"); // NOI18N

        addNewGoal.setText("Add Goal"); // NOI18N
        addNewGoal.setName("addNewGoal"); // NOI18N

        AgentTypeName.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        AgentTypeName.setText("Agent Type Name"); // NOI18N
        AgentTypeName.setName("AgentTypeName"); // NOI18N

        deleteGoalButton.setText("Delete Goal"); // NOI18N
        deleteGoalButton.setName("deleteGoalButton"); // NOI18N

        agentTypeNameFiled.setName("agentTypeNameFiled"); // NOI18N

        newGoalField.setName("newGoalField"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        goalsList_.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "...." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        goalsList_.setName("goalsList_"); // NOI18N
        jScrollPane1.setViewportView(goalsList_);

        saveToFileButton.setText("Save to file"); // NOI18N
        saveToFileButton.setName("saveToFileButton"); // NOI18N

        javax.swing.GroupLayout leftPanelLayout = new javax.swing.GroupLayout(leftPanel);
        leftPanel.setLayout(leftPanelLayout);
        leftPanelLayout.setHorizontalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addNewGoal, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(deleteGoalButton, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(saveToFileButton, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(AgentTypeName, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(agentTypeNameFiled, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                    .addComponent(newGoalField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE))
                .addContainerGap())
        );
        leftPanelLayout.setVerticalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(agentTypeNameFiled, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AgentTypeName, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(addNewGoal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(newGoalField))
                .addGap(18, 18, 18)
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, leftPanelLayout.createSequentialGroup()
                        .addComponent(deleteGoalButton, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                        .addComponent(saveToFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
                .addContainerGap())
        );

        addTypeButton.setText("AddType"); // NOI18N
        addTypeButton.setName("addTypeButton"); // NOI18N

        jLabel1.setText("AGENT TYPE CREATOR"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        close.setText("Close"); // NOI18N
        close.setName("close"); // NOI18N
        close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(addTypeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(close))
                            .addComponent(leftPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(leftPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addTypeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(close, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeActionPerformed
        this.dispose();
    }//GEN-LAST:event_closeActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AgentTypeName;
    private javax.swing.JButton addNewGoal;
    private javax.swing.JButton addTypeButton;
    private javax.swing.JTextField agentTypeNameFiled;
    private javax.swing.JButton close;
    private javax.swing.JButton deleteGoalButton;
    private javax.swing.JList goalsList_;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JTextField newGoalField;
    private javax.swing.JButton saveToFileButton;
    // End of variables declaration//GEN-END:variables
}
